package com.opi.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.log4j.Logger;

import com.opi.util.ConnDB;


public class BaseDao {

	public Connection conn= null;
	public PreparedStatement pstm = null;
	public ResultSet res,res2 = null;
	private Logger logger = Logger.getLogger(BaseDao.class);
	
	
	
	public  ResultSet query(String sql) {
		conn = ConnDB.getConn();
		try {
			pstm = conn.prepareStatement(sql);
			res = pstm.executeQuery();
		} catch (SQLException e) {
			logger.error("query方法出错："+e);
		}
		return res;
	}
	
	public int getCount (String tableName){
		String sql = "select count(*) count from "+tableName;
		Integer count = 0;
		conn = ConnDB.getConn();
		res = query(sql);
		try {
			if(res.next()){
				count = res.getInt(1);
			}
		} catch (SQLException e) {
			logger.error("getCount方法出错："+e);
		}
		this.closeDB();
		return count;
	} 
	
	/**
	 * 增删改的sql
	 * @param sql
	 * @return
	 */
	public  boolean adu(String sql) {
		conn = ConnDB.getConn();
		int m=0;
		try {
			pstm = conn.prepareStatement(sql);
			m = pstm.executeUpdate();
		} catch (SQLException e) {
			logger.error("adu方法出错："+e);
		}
		this.closeDB();
		if(m>0) 
			return true;
		else
			return false;
	}  
	
	
	public void closeDB(){
		try {
			if(!this.conn.isClosed()&&this.conn!=null) 
				this.conn.close();
		} catch (SQLException e) {
				logger.error(e);
			}
	}
}
